import Dep from './dep.js';
let $uid = 0
export default class Watcher {
    constructor(exp, scope, callback) {
        this.exp = exp;
        this.scope = scope;
        this.callback = callback;
        this.uid = $uid++;
        // 执行更新操作
        this.update();
    }

    // 更新方法
    update() {
        // 获取表达式最新的状态数据
        const value = this.get();
        // 执行回调函数更新节点内容
        this.callback(value);
    }

    // 获取表达式的最新值
    get() {
        Dep.target = this;
        let newValue = Watcher.computedExpression(this.scope,this.exp);
        Dep.target = null;
        return newValue;
    }
    static computedExpression(scope,exp){
        const fn=new Function('scope','with(scope){ return '+exp+'}');
        return fn(scope);
    }
}